package com.yfl.tingyu.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yfl.tingyu.pojo.Admin;
import com.yfl.tingyu.pojo.Menu;
import com.yfl.tingyu.service.impl.MenuServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-05-26
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuServiceImpl menuService;


    @RequestMapping("/menuManager")
    public String menuManager(){

        return "menuManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Menu> selectAdminMenu(HttpSession session){

        //通过session获取登录的用户id
        Admin admin = (Admin)session.getAttribute("SESSION_ADMIN");

        Integer aid = admin.getAid();

        List<Menu> menus = menuService.selectAdminMenu(aid);

        return  menus;
    }

    @RequestMapping("/allList")
    @ResponseBody
    public List<Menu> selectMenu(){

        List<Menu> menus = menuService.allList();

        return  menus;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Boolean saveOrUpdate(Menu menu){

        System.out.println("参数"+menu.getPid());
        boolean saveOrUpdate = menuService.saveOrUpdate(menu);
        return saveOrUpdate;
    }
    @RequestMapping("/delMenu")
    @ResponseBody
    public Boolean delMenu(Integer mid){

        QueryWrapper<Menu> qw=new QueryWrapper<>();
        //如果删除的是父菜单，子菜单一起删除
        qw.eq("mid",mid).or().eq("pid",mid);

        boolean del = menuService.remove(qw);
        return del;
    }


}

